package com.bac.rentmap.datastore;

import java.util.logging.Logger;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Query;

/**
 * Clears datastore.
 *
 * @author Andrew Osipenko
 */
public class DatastoreCleaner {
    private static final Logger log = Logger.getLogger(DatastoreCleaner.class
            .getName());

    public void clear(String kind) {
        log.info("Clearing " + kind);
        DatastoreService datastore = DatastoreServiceFactory
                .getDatastoreService();
        Query query = new Query(kind);
        int count = datastore.prepare(query).countEntities();
        log.info("Clearing " + count + " " + kind + " entities");
        for (Entity entity : datastore.prepare(query).asIterable(
                FetchOptions.Builder.withPrefetchSize(count))) {
            datastore.delete(entity.getKey());
        }
    }
}
